package com.template.service.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.template.common.ServerResponse;
import com.template.model.Product;
import com.template.dao.ProductMapper;

import com.template.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * Title:  ProductServiceImpl
 * Description: 请填写描述信息
 * @author yls
 * @created  2020年06月10日 11:19:45 星期三
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper  productMapper;

    @Override
    public ServerResponse detail(String id) {
        Product product=productMapper.selectByPrimaryKey(id);
        return ServerResponse.createBySuccess(product);
    }

    @Override
    public ServerResponse list() {
        List<Product> list=productMapper.selectAll();
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse pageList(Integer pageNumber, Integer pageSize) {
           PageHelper.startPage(pageNumber,pageSize);
           List<Product> list=productMapper.selectAll();
           PageInfo pageResult=new PageInfo(list);
           return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse save(Product  product) {
             //创建更新时间
             product.setCreateDate(new Date());
             product.setModifyDate(new Date());
             productMapper.insert(product);
             return ServerResponse.createBySuccess("保存成功");
    }

    @Override
    public ServerResponse update(Product  product) {
            Product  current=productMapper.selectByPrimaryKey(product.getId());
            if(current==null){
                return ServerResponse.createByErrorMessage("信息不存在");
            }
            productMapper.updateByPrimaryKey(product);
            return ServerResponse.createBySuccess("更新成功");
    }

    @Override
    public ServerResponse deleteById(String id) {
          Product  current=productMapper.selectByPrimaryKey(id);
          if(current==null){
            return ServerResponse.createByErrorMessage("信息不存在");
          }
          productMapper.deleteByPrimaryKey(id);
          return ServerResponse.createBySuccess("删除成功");
    }
}
